//
//  WBWelcomeView.swift
//  SinaWeibo
//
//  Created by 美食家 on 17/4/12.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {

    @IBOutlet weak var userIcon: UIImageView!

    @IBOutlet weak var tipLabel: UILabel!
    
    
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    
   class func welcomeView()-> WBWelcomeView{
    
        let v = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        let nib = v.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        nib.frame = UIScreen.main.bounds
        return   nib 
    
    }
    
    override func awakeFromNib() {
        
        guard  let urlString = LHNetWorkManager.shared.userAccount.avatar_large,
            let url = URL(string:urlString),
            let name=LHNetWorkManager.shared.userAccount.screen_name else {
                      return
         }
        
        userIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        /** 下面这句代码头像不能出来,使用userIcon.bounds.width打印的结果是1000*/
//        userIcon.layer.cornerRadius=userIcon.bounds.width*0.5
//        print("=========\(userIcon.bounds.width)")
        userIcon.layer.cornerRadius=42.5
        userIcon.layer.masksToBounds=true
        tipLabel.text="欢迎回来," + name
    }
    
    override func didMoveToWindow() {
        self.layoutIfNeeded()
        bottomCons.constant=bounds.size.height - 200
        
        UIView.animate(withDuration: 2,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
                        
                 self.layoutIfNeeded()
                        
            }) { (_) in
                
                UIView.animate(withDuration: 1.0, animations: {
                    self.tipLabel.alpha=1
                    }, completion: { (_) in
                        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1, execute: {
                            self.removeFromSuperview()
                        })
                })
                
        }
    }
}
